package GUI;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import enums.SearchType;
import Other.Glob;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Menu extends JFrame {
	private JPanel contentPane;
	boolean disable;
	private static Menu menuInstance = null;
	private JTextField tfOption;
	/**
	 * Create the frame.
	 */
	private Menu() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		JLabel lblCountry = new JLabel("1. Country");
		JLabel lblpayment = new JLabel("2. Payment");
		JLabel lblSelectOption = new JLabel("Select Option");
		tfOption = new JTextField();
		tfOption.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				//when a number is entered with ENTER
					if(e.getKeyChar() == KeyEvent.VK_ENTER){
						if(disable == false){
						if(tfOption.getText().equals("1")){
							
					Country.getInstance().setVisible(true);
					Menu.getInstance().disableMenu();
				}
						else if(tfOption.getText().equals("2")){
							
					Payment.getInstance().setVisible(true);
					Menu.getInstance().disableMenu();
						}
						else if(tfOption.getText().equals("3")){
							
					Incoterm.getInstance().setVisible(true);
					Menu.getInstance().disableMenu();
						}
						else if(tfOption.getText().equals("4")){
							
					Tax.getInstance().setVisible(true);
					Menu.getInstance().disableMenu();
						}
						else if(tfOption.getText().equals("5")){
							
					Addressee.getInstance().setVisible(true);
					Menu.getInstance().disableMenu();
						}
						else if(tfOption.getText().equals("6")){
							
					Supplier.getInstance().setVisible(true);
					Menu.getInstance().disableMenu();
						}
						else if(tfOption.getText().equals("7")){
			
					Client.getInstance().setVisible(true);
					Menu.getInstance().disableMenu();
						}
						else if(tfOption.getText().equals("8")){
							
							Destination.getInstance().setVisible(true);
							Menu.getInstance().disableMenu();
								}
						else if(tfOption.getText().equals("9")){
							
							Shipment.getInstance().setVisible(true);
							
							Menu.getInstance().disableMenu();
								}
						else if(tfOption.getText().equals("10")){
							
							Transport.getInstance().setVisible(true);
							
							Menu.getInstance().disableMenu();
								}
else if(tfOption.getText().equals("11")){
							
							AddShipment.getInstance().setVisible(true);
							
							Menu.getInstance().disableMenu();
								}
						
						else{
					JOptionPane.showMessageDialog(null, "Enter a valid option from the screen");
					tfOption.setText("");
				
				}
						}
				}
				System.out.println("Enter is being pressed");
			}
		});
		//set up Escape for exit
		tfOption.getInputMap(tfOption.WHEN_IN_FOCUSED_WINDOW)
		.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "");
		tfOption.getActionMap().put("", new AbstractAction() {
			 public void actionPerformed(ActionEvent e) {
				if (disable == false){
				//escape from the screen
					escape();
				}
			    }
		});
		tfOption.setColumns(10);
		
		JLabel lblIncoterm = new JLabel("3. Incoterm");
		
		JLabel lblTax = new JLabel("4. Tax");
		
		JLabel lbladdressee = new JLabel("5. Addressee");
		
		JLabel lblSupplier = new JLabel("6. Supplier");
		
		JLabel lblclient = new JLabel("7. Client");
		
		JLabel lblDestination = new JLabel("8. Destination");
		
		JLabel lblShipment = new JLabel("9. Shipment");
		
		JLabel lblTransport = new JLabel("10. Transport");
		
		JLabel lblAddShipment = new JLabel("11. Add Shipment to Transport");
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(34)
							.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
								.addComponent(lblTax)
								.addComponent(lbladdressee)
								.addComponent(lblSupplier)
								.addComponent(lblclient)
								.addComponent(lblDestination)
								.addGroup(gl_contentPane.createSequentialGroup()
									.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
										.addComponent(lblCountry, GroupLayout.PREFERRED_SIZE, 85, GroupLayout.PREFERRED_SIZE)
										.addComponent(lblpayment)
										.addComponent(lblIncoterm))
									.addGap(42)
									.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
										.addComponent(lblAddShipment)
										.addComponent(lblTransport)
										.addComponent(lblShipment)))))
						.addGroup(gl_contentPane.createSequentialGroup()
							.addGap(106)
							.addComponent(lblSelectOption)
							.addGap(18)
							.addComponent(tfOption, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(150, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(21)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblCountry)
						.addComponent(lblShipment))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblpayment)
						.addComponent(lblTransport))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblIncoterm)
						.addComponent(lblAddShipment))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(lblTax)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(lbladdressee)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(lblSupplier)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(lblclient)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(lblDestination)
					.addPreferredGap(ComponentPlacement.RELATED, 57, Short.MAX_VALUE)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblSelectOption)
						.addComponent(tfOption, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
		);
		contentPane.setLayout(gl_contentPane);
	}
	public void enableMenu(){
		Menu.getInstance().enable();
		disable = false;
		tfOption.setText(null);
		tfOption.requestFocus();
		Menu.getInstance().toFront();
	}
	public void disableMenu(){
		Menu.getInstance().disable();
		disable = true;
	}
public static Menu getInstance(){
		if(menuInstance == null)
			menuInstance = new Menu();
		return menuInstance;
		}
//method to close the screen
public void escape(){
	Menu.getInstance().dispose();
}
}